/*
*
==============================================================================
*  Name        : 
*  Part of     : Omen Game Engine - ZIP Archive Plugin
*  Description : 
*  Version     : 1.0
*
*  Copyright (c) 2013 Lauri Kortevaara.
* 
==============================================================================
*/

#define ZIPPLUGIN_SOURCE 1

#include "Config.h"
#include <OmenEngine>

#include <stdexcept>

using namespace std;

namespace Omen {

    class ZipArchive : public IArchive {
    public:
        virtual ~ZipArchive() {}
    };

  /// Zip archive reader
  class ZipArchiveReader : public IArchiveReader {

    /// <summary>Destroys a zip archive reader</summary>
    public: ZIPPLUGIN_API virtual ~ZipArchiveReader() {}

    /// <summary>
    ///   Checks whether the archive reader can open an archive
    /// </summary>
    /// <param name="filename">Archive that will be tested</param>
    public: ZIPPLUGIN_API bool canOpenArchive(const wstring &filename) {
      return filename.find(L".zip") == (filename.length() - 4);
    }

    /// <summary>Opens an archive for reading</summary>
    /// <param name="filename">Archive that will be opened</param>
    public: ZIPPLUGIN_API IArchive* openArchive(
      const wstring &sFilename
    ) {
      if(!canOpenArchive(sFilename)) {
        throw runtime_error("No Zip archive");
      }

      return new ZipArchive;
    }

  };

  /// <summary>Retrieves the engine version we're expecting</summary>
  /// <returns>The engine version the plugin was built against</returns>
  extern "C" ZIPPLUGIN_API int getEngineVersion() {
    return 1;
  }

  /// <summary>Registers the plugin to an engine kernel</summary>
  /// <param name="kernel">Kernel the plugin will be registered to</param>
  extern "C" ZIPPLUGIN_API void registerPlugin( Omen::IKernel* kernel) {
    kernel->getStorageServer()->addArchiveReader(
      new ZipArchiveReader()
    );
  }

} // namespace Omen
